<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>js向CSS传值</title>
</head>

<body>
    <style>
        .b {
            color: var(--theme-color);
        }
    </style>
    <div class="a">a
        <div class="b">b
            <div class="c">c
                <div class="d">d</div>
            </div>
        </div>
    </div>
    <script>
        const aEl = document.querySelector('.a');
        aEl.style.setProperty('--theme-color', 'red');
    </script>
</body>

</html>